package kr.hyunist.musicplayproject.UTIL;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.net.Uri;
import android.nfc.Tag;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;

public class Util {
	static String TAG = "MEDIA_TAG";
	static int MY_PLAYLIST_ID = -1;
	static final String MYPLAYLIST_NAME = "MyPlayList";
	static final String[] STAR = { "*" };
	static final String PREFS_NAME = "MyVolumeSetting";
	static boolean isMute;
	static int vol;
	static SharedPreferences settings;
	static Context context = null;
	static AudioManager am = null;

	
	private static final BitmapFactory.Options sBitmapOptionsCache = new BitmapFactory.Options();
	private static final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");

	public static Bitmap getArtworkQuick(Context context, int album_id, int w, int h) {

		w -= 2;
		h -= 2;
		ContentResolver res = context.getContentResolver();
		Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
		if (uri != null) {
			ParcelFileDescriptor fd = null;
			try {
				fd = res.openFileDescriptor(uri, "r");
				int sampleSize = 1;
				sBitmapOptionsCache.inJustDecodeBounds = true;
				BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(),
						null, sBitmapOptionsCache);
				int nextWidth = sBitmapOptionsCache.outWidth >> 1;
				int nextHeight = sBitmapOptionsCache.outHeight >> 1;
				while (nextWidth > w && nextHeight > h) {
					sampleSize <<= 1;
					nextWidth >>= 1;
					nextHeight >>= 1;
				}

				sBitmapOptionsCache.inSampleSize = sampleSize;
				sBitmapOptionsCache.inJustDecodeBounds = false;
				Bitmap b = BitmapFactory.decodeFileDescriptor(
						fd.getFileDescriptor(), null, sBitmapOptionsCache);

				if (b != null) {
					if (sBitmapOptionsCache.outWidth != w
							|| sBitmapOptionsCache.outHeight != h) {
						Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true);
						b.recycle();
						b = tmp;
					}
				}

				return b;
			} catch (FileNotFoundException e) {
			} finally {
				try {
					if (fd != null)
						fd.close();
				} catch (IOException e) {
				}
			}
		}
		return null;
	}
		
	//밀리초를 시,분,초 단위로 변경
	public static String milliSecToTime(String millisec) {
		int hour;
		int sec;
		int min;

		if (millisec == null || millisec.equals("") || millisec.equals("0"))
			return "";

		sec = Integer.parseInt(millisec) / 1000;
		hour = sec / 60 / 60;
		min = sec / 60 % 60;
		sec = sec % 60 % 60;

		if (hour > 0)
			return String.format("%2d:%02d:%02d", hour, min, sec);
		else
			return String.format("%2d:%02d", min, sec);
	}
	
	//플레이리스트의 ID를 받아온다.
	public static int idForPlayList(Context context, String myPlayListName) {
		Cursor c = query(context,
				MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
				new String[] { MediaStore.Audio.Playlists._ID },
				MediaStore.Audio.Playlists.NAME + "=?",
				new String[] { myPlayListName },
				MediaStore.Audio.Playlists.NAME);

		int myPlayListID = intFromCursor(c);

		if (myPlayListID == -1)
			return writePlaylist(context, myPlayListName);
		else
			return (myPlayListID);

	}
	
	//플레이리스트가 없을경우 플레이리스트를 만들기 위한 메소드
	public static int writePlaylist(Context context, String myPlayListName) {

		int myPlayListID;
		ContentResolver resolver = context.getContentResolver();

		ContentValues values = new ContentValues(1);
		values.put(MediaStore.Audio.Playlists.NAME, myPlayListName);
		resolver.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values);
		myPlayListID = idForPlayList(context, myPlayListName);

		return myPlayListID;
	}
	
	public static Cursor query(Context context, Uri uri, String[] projection,
			String selection, String[] selectionArgs, String sortOrder) {
		return query(context, uri, projection, selection, selectionArgs,
				sortOrder, 0);
	}
	
	public static Cursor query(Context context, Uri uri, String[] projection,
			String selection, String[] selectionArgs, String sortOrder,
			int limit) {
		try {
			ContentResolver resolver = context.getContentResolver();
			if (resolver == null) {
				return null;

			}
			if (limit > 0) {
				uri = uri.buildUpon().appendQueryParameter("limit", "" + limit)
						.build();
			}
			return resolver.query(uri, projection, selection, selectionArgs,
					sortOrder);
		} catch (UnsupportedOperationException ex) {
			return null;
		}
	}
	
	private static int intFromCursor(Cursor c) {
		int id = -1;
		if (c != null) {
			c.moveToFirst();

			if (!c.isAfterLast()) {
				id = c.getInt(0);
			}
		}

		if (c != null)
			c.close();

		return id;
	}
	
	//볼륨 제어를 위한 AudioManager 객체를 생성
	public static void init(Context c) {
		context = c;
		settings = c.getSharedPreferences(PREFS_NAME, 0);
		isMute = settings.getBoolean("IsMute", false);
		am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		if (isMute) {
			am.setStreamMute(AudioManager.STREAM_MUSIC, true);
		} else {
		}
		
		vol = am.getStreamVolume(AudioManager.STREAM_MUSIC);

	}

	public static boolean isMute() {
		return isMute;
	}

	public static int getVol() {
		return vol;
	}

	public static int setVolUp() {
		am.setStreamVolume(AudioManager.STREAM_MUSIC, vol + 1,
				AudioManager.FLAG_PLAY_SOUND);
		vol = am.getStreamVolume(AudioManager.STREAM_MUSIC);
		return vol;
	}

	public static int setVolDown() {
		am.setStreamVolume(AudioManager.STREAM_MUSIC, vol - 1,
				AudioManager.FLAG_PLAY_SOUND);
		vol = am.getStreamVolume(AudioManager.STREAM_MUSIC);
		return vol;
	}

	public static void destroy() {
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean("IsMute", isMute);
		editor.commit();

		if (isMute) {
			am.setStreamMute(AudioManager.STREAM_MUSIC, false);
			isMute = false; // 음소거 해제
			vol = am.getStreamVolume(AudioManager.STREAM_MUSIC);
		}

	}
	
	//플레이리스트에 곡 추가
	public static void addToPlayList(ContentResolver resolver, int audioId,
			int myPlayListID) {
		String[] cols = new String[] { "count(*)" };
		Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external",
				myPlayListID);
		Cursor cur = resolver.query(uri, cols, null, null, null);
		cur.moveToFirst();
		final int base = cur.getInt(0);
		cur.close();
		ContentValues values = new ContentValues();

		values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER,
				Integer.valueOf(base + audioId));
		values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audioId);
		resolver.insert(uri, values);

	}
	
	//플레이리스트 안의 곡 삭제
	public static void removeFromPlaylist(ContentResolver resolver, int id,
			int myPlayListID) {
		String[] cols = new String[] { "count(*)" };
		Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external",
				myPlayListID);
		Cursor cur = resolver.query(uri, cols, null, null, null);
		cur.moveToFirst();
		cur.close();
		resolver.delete(uri, MediaStore.Audio.Playlists.Members._ID + " = "
				+ id, null);
	}
	
	/**
	 * @Method : getRealPathFromURI
	 * @Description : uri로 path를 구하기 위한 메소드
	 * @param : uri, context
	 */
	public static String getRealPathFromURI(Uri contentUri, Context mContext) {
		try {
			// can post image
			String[] proj = { MediaStore.Images.Media.DATA };

			Cursor cursor = mContext.getContentResolver().query(contentUri,
					proj, // Which columns to return
					null, // WHERE clause; which rows to return (all rows)
					null, // WHERE clause selection arguments (none)
					null); // Order-by clause (ascending by name)

			int column_index = cursor
					.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

			cursor.moveToFirst();

			return cursor.getString(column_index);
		} catch (IllegalArgumentException e) {
			return "";
		} catch (CursorIndexOutOfBoundsException e) {
			return "";
		} catch (Exception e) {
			return "";
		}
	}
	
	/**
	 * @Method : getLyricsTagRead
	 * @Description : 음악파일의 노래가사를 얻어오는 메소드
	 * @param : 음악파일 저장경로
	 * @return : 노래가사
	 */
	/*public static String getLyricsTagRead(String path) {

		String lyric = "";

		try {
			File file = new File(path);
			AudioFile audioFile = AudioFileIO.read(file);
			Tag tag = audioFile.getTag();
			lyric = tag.getFirst(FieldKey.LYRICS);

		} catch (Exception e) {
			lyric = "";
		}
		return lyric;

	}*/
}
